home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GDEVDJTC.C < prev    next >
C/C++ Source or Header  |  1992-03-23  |  6KB  |  173 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevdjtc.c */
  21. /* HP DeskJet 500C driver for Ghostscript */
  22. #include "gdevprn.h"
  23. #include "gdevpcl.h"
  24.  
  25. /***
  26.  *** Note: this driver was contributed by a user, Alfred Kayser:
  27.  ***       please contact AKayser@et.tudelft.nl if you have questions.
  28.  ***/
  29.  
  30. #define X_DPI 300
  31. #define Y_DPI 300
  32. /* bytes per line for DeskJet Color */
  33. #define LINE_SIZE ((X_DPI * 85 / 10 + 63) / 64 * 8)
  34.  
  35. /* The device descriptors */
  36. private dev_proc_print_page(djet500c_print_page);
  37.  
  38. private gx_device_procs djet500c_procs =
  39.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  40.     gdev_pcl_3bit_map_rgb_color, gdev_pcl_3bit_map_color_rgb);
  41.  
  42. gx_device_printer gs_djet500c_device =
  43.   prn_device(djet500c_procs, "djet500c",
  44.     85,                /* width_10ths, 8.5" */
  45.     120,                /* height_10ths, 12" */
  46.     X_DPI, Y_DPI,
  47.     0.25, 0.25, 0.25, 0.25,        /* margins */
  48.     3, djet500c_print_page);
  49.  
  50. /* Forward references */
  51. private int djet500c_print_page(P2(gx_device_printer *, FILE *));
  52.  
  53. /* The DeskJet 500C uses additive colors in separate planes. */
  54. /* We only keep one bit of color, with 1 = R, 2 = G, 4 = B. */
  55. /* Because the buffering routines assume 0 = white, */
  56. /* we complement all the color components. */
  57. #define cv_shift (sizeof(gx_color_value) * 8 - 1)
  58.  
  59. /* Map an RGB color to a printer color. */
  60. private gx_color_index
  61. djet500c_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  62.   gx_color_value b)
  63. {    return (((b >> cv_shift) << 2) + ((g >> cv_shift) << 1) + (r >> cv_shift)) ^ 7;
  64. }
  65.  
  66. /* Map the printer color back to RGB. */
  67. private int
  68. djet500c_map_color_rgb(gx_device *dev, gx_color_index color,
  69.   gx_color_value prgb[3])
  70. {    ushort cc = (ushort)color ^ 7;
  71.     prgb[0] = -(cc & 1);
  72.     prgb[1] = -((cc >> 1) & 1);
  73.     prgb[2] = -(cc >> 2);
  74.     return 0;
  75. }
  76.  
  77. /* Send the page to the printer.  For speed, compress each scan line, */
  78. /* since computer-to-printer communication time is often a bottleneck. */
  79. /* The DeskJet Color can compress (mode 3) */
  80.  
  81. private int
  82. djet500c_print_page(gx_device_printer *pdev, FILE *fprn)
  83. {
  84. #define DATA_SIZE (LINE_SIZE*8)
  85.     byte data[DATA_SIZE];
  86.     byte plane_data[DATA_SIZE];
  87.  
  88.     /* select the most compressed mode available & clear tmp storage */
  89.     /* put printer in known state */
  90.     fputs("\033E",fprn);
  91.     /* ends raster graphics to set raster graphics resolution */
  92.     fputs("\033*rbC", fprn);    /*  was \033*rB  */
  93.  
  94.     /* set raster graphics resolution -- 300 dpi */
  95.     fputs("\033*t300R", fprn);
  96.     /* move to top left of page & set current position */
  97.     fputs("\033*p0x0Y", fprn); /* cursor pos: 0,0 */
  98.     fputs("\033*r0A", fprn);  /* start graf. left */
  99.     fputs("\033*b2M", fprn);    /*  mode 3 compression for now  */
  100.     fputs("\033*r3U", fprn);    /* RGB Mode */
  101.     fputs("\033&l26a0l1H", fprn); /* A4, skip perf, def. paper tray */ 
  102.  
  103.     /* Send each scan line in turn */
  104.        {    int lnum;
  105.         int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  106.         int num_blank_lines = 0;
  107.         for (lnum=0; lnum<pdev->height; lnum++)
  108.         {    
  109.             byte _ss *end_data = data + line_size;
  110.             gdev_prn_copy_scan_lines(pdev, lnum, (byte *)data, line_size);
  111.  
  112.             /* Remove trailing 0s. */
  113.             while ( end_data > data && end_data[-1] == 0 )
  114.                 end_data--;
  115.             if ( end_data == data )
  116.                 num_blank_lines++;
  117.             else
  118.             {    int i;
  119.                 byte _ss *odp;
  120.                 byte _ss *row;
  121.  
  122.                 /* Pad with 0s to fill out the last */
  123.                 /* block of 8 bytes. */
  124.                 memset(end_data, 0, 7);
  125.  
  126.                 /* Transpose the data to get pixel planes. */
  127.                 for (i=0, odp=plane_data; i<DATA_SIZE; i+=8, odp++)
  128.                 { 
  129.                    register ushort t, r, g, b;
  130.                    for (r=g=b=t=0;t<8;t++)
  131.                    {
  132.                         r = (r<<1) | (data[t+i]&4);
  133.                         g = (g<<1) | (data[t+i]&2);
  134.                         b = (b<<1) | (data[t+i]&1);
  135.                    }
  136.                    odp[0] = (byte)r ^ 0xff;
  137.                    odp[LINE_SIZE] = (byte)(g>>1) ^ 0xff;
  138.                    odp[LINE_SIZE*2] = (byte)(b>>2) ^ 0xff;
  139.                 }
  140.  
  141.                 /* Skip blank lines if any */
  142.                 if ( num_blank_lines > 0 )
  143.                 {    /* move down from current position */
  144.                     fprintf(fprn, "\033*b%dY", num_blank_lines);
  145.                     num_blank_lines = 0;
  146.                 }
  147.  
  148.                 /* Transfer raster graphics */
  149.                 /* in the order R, G, B. */
  150.                 row=plane_data+LINE_SIZE*2;
  151.                 for (i=0; i<3; row-=LINE_SIZE,i++)
  152.                 {    
  153.                     byte temp[LINE_SIZE * 2];
  154.                     int count = gdev_pcl_mode2compress((word *)row, (word *)(row + LINE_SIZE), temp);
  155.                     fprintf(fprn, "\033*b%d%c", count, "VVW"[i]);
  156.                     fwrite(temp, sizeof(byte), count, fprn); 
  157.                 }
  158.             }
  159.         }
  160.     }
  161.     /* end raster graphics */
  162.     fputs("\033*rbC", fprn);    /*  was \033*rB  */
  163.     fputs("\033*r1U", fprn);    /*  back to 1 plane  */
  164.  
  165.        /* put printer in known state */
  166.     fputs("\033E",fprn);
  167.     
  168.     /* eject page */
  169.     fputs("\033&l0H", fprn);
  170.  
  171.     return 0;
  172. }
  173.